﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace VeryGoodSync
{
    /// <summary>
    /// 全局帮助方法
    /// </summary>
    public static class Ui
    {
        internal static event Action<string, string, ToolTipIcon, int> ShowNoticeRequest;

        internal static event Action<bool> ControlAnimationRequest;

        /// <summary>
        /// 显示错误信息
        /// </summary>
        /// <param name="ex"></param>
        public static void ShowError(Exception ex)
        {
            ShowNoticeRequest?.Invoke("错误", ex.Message, ToolTipIcon.Error, 10000);
        }

        /// <summary>
        ///  显示普通提醒消息
        /// </summary>
        /// <param name="message"></param>
        public static void ShowInfo(string message)
        {
            ShowNoticeRequest?.Invoke("提醒", message, ToolTipIcon.Info, 3000);
        }

        /// <summary>
        /// 显示成功消息
        /// </summary>
        /// <param name="message"></param>
        public static void ShowSuccess(string message)
        {
            ShowNoticeRequest?.Invoke("成功", message, ToolTipIcon.Info, 3000);
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="action"></param>
        /// <param name="ex"></param>
        public static void TraceError(string action, Exception ex)
        {
            Log.WriteMessage(action, ex);
        }

        internal static void StartAnimation()
        {
            ControlAnimationRequest?.Invoke(true);
        }

        internal static void StopAnimation()
        {
            ControlAnimationRequest?.Invoke(false);
        }

        /// <summary>
        /// 获取文件的MD5 Hash值
        /// </summary>
        /// <param name="filePath">目标文件路径</param>
        /// <returns></returns>
        public static string GetFileMd5(string filePath)
        {
            if (!File.Exists(filePath)) return null;
            var hash = SHA1.Create();
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                byte[] hashByte = hash.ComputeHash(stream);
                stream.Close();
                return BitConverter.ToString(hashByte).Replace("-", "");
            }
        }
    }
}
